package net.xdclass.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import net.xdclass.request.CartItermRequest;
import net.xdclass.service.CartService;
import net.xdclass.util.JsonData;
import net.xdclass.vo.CartItemVO;
import net.xdclass.vo.CartVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author : zhen.xue
 * @date : 2021-05-28 16:09
 */
@Api(tags = "购物车")
@RestController
@RequestMapping("/api/cart/v1")
public class CratController {

    @Autowired
    private CartService cartService;


    @ApiOperation("添加到购物车")
    @PostMapping("add")
    public JsonData addToCart(@ApiParam(value = "购物项") @RequestBody CartItermRequest cartItermRequest){
        cartService.addToCart(cartItermRequest);

        return JsonData.buildSuccess();
    }

    @ApiOperation("添加到购物车")
    @PostMapping("change")
    public JsonData changeItemNum(@ApiParam(value = "购物项") @RequestBody CartItermRequest cartItermRequest){
        cartService.changeItemNum(cartItermRequest);

        return JsonData.buildSuccess();
    }


    @ApiOperation("清空购物车")
    @DeleteMapping("/clear")
    public JsonData clearMyCar(){
        cartService.clear();
        return JsonData.buildSuccess();
    }


    @ApiOperation("查看我的购物车")
    @GetMapping("/mycart")
    public JsonData findMyCart(){
        CartVO cartVO = cartService.findMyCart();
        return JsonData.buildSuccess(cartVO);
    }


    @ApiOperation("删除购物项")
    @DeleteMapping("/delete/{product_id}")
    public JsonData deleteItem(@ApiParam(value = "商品id",required = true) @PathVariable("product_id") long productId){
        cartService.deleteItem(productId);
        return JsonData.buildSuccess();
    }


    /**
     * 用于订单服务，确认订单，获取对应的商品信息详情
     *
     * 会清空购物车的商品数据局
     * @param productIdList
     * @return
     */
    @ApiOperation("获取对应订单的商品信息")
    @PostMapping("/confirm_order_cart_items")
    JsonData confirmOrderCartItems(@ApiParam(value = "商品id列表") @RequestBody List<Long> productIdList){
        List<CartItemVO> list= cartService.confirmOrderCartItems(productIdList);
        return JsonData.buildSuccess(list);
    }
}